<html>
   <head>
		<meta charset="utf-8"/>
		<title>Box2dWeb的关节演示</title>
   </head>
   <body>
	<script type="text/javascript" src="../libs/minicomps.min.js"></script>
	<script type="text/javascript" src="../libs/Box2dWeb-2.1.a.3.min.js"></script>
	<script type="text/javascript" src="../libs/moonBox2d.js"></script>
		<script type="text/javascript">
			const { Toggle, HSlider, Panel, Canvas ,Button,TextArea,TextBox,TextInput,Label,Window,Image,LabelInput,TipsManager,RadioButton,ColorPicker,TabbarWindow,Checkbox,Stage} = mc
			const width = Stage.width-40;
			const height = Stage.height-40;
			const [minW,minH]=[width/3,height/3];
			const parent=new Canvas(document.body, 10, 10, Stage.width-20,Stage.height-20);
			const mbs=[];
			const labels=['距离关节','焊接关节','平移关节与凹凸多边形','马达关节','线段关节','齿轮关节','使用马达关节与焊接关节制作固定锁链','滑轮关节','创建马达关节与焊接关节(制作小汽车)','','','','']
			const mbTotal=12;
			for(let i=0;i<mbTotal;i++){
				let	[x,y]=[parseInt(i%3)*(minW+5)+5,parseInt(i/3)*(minH+5)+5];
				let canvas = new Canvas(parent, x, y, minW,minH);
				let mb=new MoonBox2d();
				mb.box2dInit(canvas,1);
				mb.openMouseEvent();
				new Label(canvas,10,10,labels[i])
				mbs.push(mb);
			}
			for(let i=0;i<mbTotal;i++){
				let fun=window['mb'+i];
				log(fun)
				if(!!fun){fun(mbs[i])}
			}
			
			function mb0(mb){//距离关节
				var box1=mb.createCircle(100,100,13);
				var box2=mb.createCircle(180,100,13);
				var jointDef = new b2DistanceJointDef();
				jointDef.Initialize(box1,box2,box1.GetWorldCenter(),box2.GetWorldCenter());
				mb.world.CreateJoint(jointDef);
			}
			
			function mb1(mb){//焊接关节
				var rect1=mb.createRect(100,100,4,20);
				var rect2=mb.createRect(160,100,4,20);
				var rect3=mb.createRect(130,120,30,4);
				var jointDef = new b2WeldJointDef();
				jointDef.Initialize(rect1,rect3,rect3.GetWorldCenter());
				mb.world.CreateJoint(jointDef);
				jointDef.Initialize(rect2,rect3,rect3.GetWorldCenter());
				mb.world.CreateJoint(jointDef);
			}
			
			function mb2(mb){//平移关节与凹凸多边形
				var box1=mb.world.GetGroundBody();
				var box2=mb.createBasket(200,100);
				var vec1=mb.b2Vec2(100,100);
				var vec2=mb.b2Vec2(1,0);
				var jointDef = new b2PrismaticJointDef();
				jointDef.Initialize(box1,box2,vec1,vec2);
				jointDef.enableMotor=true;
				jointDef.maxMotorForce=200;
				jointDef.motorSpeed=0;
				mb.world.CreateJoint(jointDef);
			}

			function mb3(mb){//创建马达关节(制作电风扇)
				var box1=mb.createCircle(200,100,25);
				var box2=mb.createRect(200,150,55,10);
				box2.SetType(0)
				var jointDef = new b2RevoluteJointDef();
				//用bodyA、bodyB和anchor节点初始化马达关节
				jointDef.Initialize( box1, box2, new b2Vec2(box1.GetPosition().x,box1.GetPosition().y));
				//设置连接的两个刚体之间不进行碰撞检测
				jointDef.collideConnected = false;
				//开启马达
				jointDef.enableMotor = true;
				//设置马达的最大角速度，单位为 弧度/秒，如设置为Math.PI，即每秒钟转180度
				jointDef.motorSpeed = Math.PI*10;
				//设置最大的扭力值
				jointDef.maxMotorTorque = 50;
				//创建马达关节
				mb.world.CreateJoint(jointDef);
			}

			function mb4(mb){//线段关节
				var box1=mb.createCircle(100,30,13);
				var box2=mb.createCircle(160,30,13);
				box1.SetType(0)
				var vec1=box1.GetPosition();
				var vec2=mb.b2Vec2(1,0)
				log(vec1,vec2)
				var jointDef = new b2LineJointDef();
				//jointDef.length=5;
				jointDef.Initialize(box1,box2,vec1,vec2,80/30);
				mb.world.CreateJoint(jointDef);
			}
			
			function mb5(mb){//齿轮关节
				var gear1=mb.createCircle(100,50,13);
				var gear2=mb.createCircle(160,50,20);
				var jointDef = new b2RevoluteJointDef();
				jointDef.Initialize(mb.world.GetGroundBody(),gear1,gear1.GetWorldCenter());
				var joint1 = mb.world.CreateJoint(jointDef)
				jointDef.Initialize(mb.world.GetGroundBody(),gear2,gear2.GetWorldCenter());
				var joint2 = mb.world.CreateJoint(jointDef)
				var gearJointDef = new b2GearJointDef();
				gearJointDef.bodyA = gear1;
				gearJointDef.bodyB = gear2;
				gearJointDef.joint1 = joint1;
				gearJointDef.joint2 = joint2;
				gearJointDef.ratio = 1;
				mb.world.CreateJoint(gearJointDef);
			}
			
			function mb6(mb){//使用马达关节与焊接关节制作固定锁链
				var joint=mb.createChain({w:24,h:4,n:6,x:50,y:100,p:4});//{w:50,h:10,n:4}
				log(joint);
				
				var rect1=mb.createRect(50,100,1,1);
				var rect2=mb.createRect(300,100,1,1);
				rect1.SetType(0);
				rect2.SetType(0);
				var jointDef = new b2WeldJointDef();
				jointDef.Initialize(rect1,joint.fristBody,rect1.GetWorldCenter());
				mb.world.CreateJoint(jointDef);
				jointDef.Initialize(rect2,joint.bodyB,rect2.GetWorldCenter());
				mb.world.CreateJoint(jointDef);
				
				mb.createCircle(50,50,10)
			}
			function mb7(mb){//滑轮关节
				var ball_A = mb.createCircle(100,120,20);
				var ball_B = mb.createCircle(200,120,20);//把位置为中心偏上方并设置为静态
				var anchor1 = ball_A.GetWorldCenter();  
				var anchor2 = ball_B.GetWorldCenter();  
				var groundAnchor1 = new b2Vec2(anchor1.x, anchor1.y - (100 / mb.miToPixel));
				var groundAnchor2 = new b2Vec2(anchor2.x, anchor2.y - (100 / mb.miToPixel));
				var ratio = 1.0;  
				var pulleyJointDef = new b2PulleyJointDef();  
				pulleyJointDef.Initialize(ball_A, ball_B, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio);  
				//pulleyJointDef.maxLengthA = 400 / mb.miToPixel;  
				//pulleyJointDef.maxLengthB = 400 / mb.miToPixel; 
				mb.world.CreateJoint(pulleyJointDef);
			}
			
			function mb8(mb){//创建马达关节与焊接关节(制作小汽车)
				//
				var box1=mb.createCircle(170,120,15);
				var box2=mb.createRect(200,100,55,10);
				var box3=mb.createCircle(230,120,15);
				var jointDef = new b2RevoluteJointDef();
				jointDef.Initialize( box1, box2, new b2Vec2(box1.GetPosition().x,box1.GetPosition().y));
				mb.world.CreateJoint(jointDef);
				jointDef.Initialize( box3, box2, new b2Vec2(box3.GetPosition().x,box3.GetPosition().y));
				mb.world.CreateJoint(jointDef);
				
				
				var varr=mb.getPolygonOtherPoints([[-58,-39],[21,-37],[30,-8],[66,-6],[69,17],[-70,15]],0.7);
				var carBody=mb.createPolygon3(100,100,varr);
				var carWhe1=mb.createCircle(120,110,12);
				var carWhe2=mb.createCircle(70,110,12);
				var jointDef = new b2RevoluteJointDef();
				jointDef.Initialize( carWhe1, carBody, new b2Vec2(carWhe1.GetPosition().x,carWhe1.GetPosition().y));
				mb.world.CreateJoint(jointDef);
				
				//设置后轮动力
				jointDef.collideConnected = false;//不进行碰撞检测
				jointDef.enableMotor = true;//开启马达
				jointDef.motorSpeed = -Math.PI*2;//设置马达的最大角速度，单位为 弧度/秒，如设置为Math.PI，即每秒钟转180度
				jointDef.maxMotorTorque = 50;//设置最大的扭力值
				jointDef.Initialize( carWhe2, carBody, new b2Vec2(carWhe2.GetPosition().x,carWhe2.GetPosition().y));
				mb.world.CreateJoint(jointDef);
				
			}
			function mb9(mb){
				var varr=mb.getPolygonOtherPoints([[-20,-20],[20,-20],[20,20],[-20,20]]);
				var body=mb.createPolygon3(100,100,varr);
			}
			
		</script>
   </body>
</html>